import QtQuick 1.1
import com.nokia.meego 1.1
import com.nokia.extras 1.1
import "storage.js" as Database
import "WebRequest.js" as Web

Page {
    id: loginPage
    tools:backtool
    signal loadStart
    signal loadStop
    property bool loading: false
    onLoadStart: { appWindow.loadStart(); loading = true }
    onLoadStop:  { appWindow.loadStop(); loading = false }

    Component.onDestruction: Web.xmlhttp.abort()
    function login(){
        loading = true;
        if (idInput.text==""||pwInput.text=="")
        {
            loading = false;
          return Web.showMessage("请输入有效的用户名或密码")

        }
        var param = { login: idInput.text, pass: pwInput.text }


        Web.sendWebRequest("POST", "http://m2.qiushibaike.com/user/signin",
                           loadLoginResult, JSON.stringify( { login: idInput.text, pass: pwInput.text }),"" )
    }
    function loadLoginResult(oritxt){
        loading = false;
        console.log(oritxt);
        var obj = JSON.parse(oritxt)
        if ( obj.err !== 0 )
            Web.showMessage(obj.err_msg);
        else{
        Database.setCache("userData", oritxt)
        Database.setSetting("qbtoken", obj.token)
        appWindow.qbtoken = obj.token
        Web.showMessage("用户"+obj.user.login+"登录成功！")
        pageStack.pop()
        }
    }

    Column {
        id: loginCol
        spacing: 10
        width: parent.width
        Row{
            width: parent.width
            height: children.height
            Rectangle {
                id: header
                anchors.top: parent.top
                width: parent.width
                height: 50
                color: "dodgerblue"
                Label {
                    anchors.centerIn: parent
                    font.bold: true
                    font.pixelSize: 35
                    color: "White"
                    text: "登陆"
                }
            }
        }
            Row {

                spacing: 10
                Label {
                    id: userName
                    text: "用户名:"
                   // font.pixelSize: platformStyle.fontSizeLarge
                    color: appWindow.colorMid
                }
                TextField {
                    id: idInput
                    width: 350
                    anchors.verticalCenter: parent.verticalCenter
                    //font.pixelSize: platformStyle.fontSizeLarge
                    placeholderText: "输入中英文ID"
                }
            }


            Row {

                spacing: 20
                Label {
                    id: pwName
                    anchors.verticalCenter: parent.verticalCenter

                    text: "密  码:"
                   // font.pixelSize: platformStyle.fontSizeLarge
                    color: appWindow.colorMid
                }
                TextField {
                    id: pwInput
                    width: idInput.width
                    echoMode: TextInput.Password

                    //font.pixelSize: platformStyle.fontSizeLarge
                    placeholderText: "必填"
                }
            }


        Row{
            id:item3
             width: parent.width
            Rectangle
            {
                width: parent.width-280-loginstatus.width
                height: parent.height
                 opacity: 0.001
             }
        Button {
            id :button3;
            width:200
            text:"登陆"
            enabled: !loading
            onClicked: {Database.initialize();login(); }
               }
        Rectangle
        {
            width: 20
            height: parent.height
             opacity: 0.001
         }
        Label {
            id:loginstatus
            text: loading? "登录中" : "提交"
            //font.pixelSize: platformStyle.fontSizeLarge
            color: appWindow.colorMid
        }
        }
        Row{
            Rectangle
            {
                width: 20
                height: parent.height
                opacity: 0.001
             }
            Text
            {

               font.pointSize: 20
               wrapMode: Text.WordWrap
                text:"<a href=\"http://www.qiushibaike.com/new4/signup\">没有账号？注册</a>"
                MouseArea{
                    anchors.fill:  parent
                    onClicked: {
                        Qt.openUrlExternally("http://www.qiushibaike.com/new4/signup");
                    }
                }
             }

        }

        Row{
            width: parent.width
            Rectangle
            {
                width: 20
                height: parent.height
                opacity: 0.001
             }
            Text
            {
               width: parent.width-50
               font.pointSize: 20
               wrapMode: Text.WordWrap
               text:"你可以使用用户名：<i>糗事百科n9</i><br /> 密码：<i>12345678</i>来测试登陆功能"
               Component.onCompleted: {
                   aboutdialog.open();

              }
             }

        }


    }

    QueryDialog {
       id: aboutdialog
       icon:"images/icon_my_active.png"
       titleText:"用户隐私"
       message:  "使用登陆功能需要您同意用户隐私策略\n本程序不会保存您的用户名和密码，所有基于登陆用户的操作（发表、评论）都是基于服务器返回的认证key"
       acceptButtonText:"接受"
       rejectButtonText: "不接受"

       onAccepted: aboutdialog.close()
       onRejected:appWindow.pageStack.pop()
        }



    ToolBarLayout {
          id: backtool
          visible: true
          ToolIcon {
              platformIconId: "toolbar-back"

              onClicked:  appWindow.pageStack.pop();
          }
      }
}
